<?php

if (!defined('BASEPATH'))exit('No direct script access allowed');
require_once ('Entity/Relax_Articles_Comments_Entity.php');
class Relax_Articles_Comments_Model extends Relax_Articles_Comments_Entity
{
    public function __construct()
    {
        parent::__construct();
    }

    
    
    public function ctotal($id)
    {
        $sql = "SELECT c.id
                FROM relax_articles_comments c 
                INNER JOIN relax_articles a ON a.id = c.aid
                WHERE a.id = ? AND a.`status` = 1 AND c.`status` = 1
            ";
        $query = $this->db->query($sql, array($id));
        return $query->num_rows();
    }
    
    public function getCommentByAid($id, $offset = 0)
    {
        $limit = 2;
        $sql = "SELECT c.*
                FROM relax_articles_comments c 
                INNER JOIN relax_articles a ON a.id = c.aid
                WHERE a.id = ? AND a.`status` = 1 AND c.`status` = 1
                LIMIT ?, $limit
                ";
        $query = $this->db->query($sql, array($id, $offset));
        return $query->result();
    }
    
    public function addComment()
    {
        $this->setFullname($this->input->post('fullname'));
        $this->setEmail($this->input->post('email'));
        $this->setContents($this->input->post('contents'));
        $this->setAid($this->input->post('vid'));
        $this->setCreated(time());
        $this->setChanged(time());
        $this->setStatus(0);
        return $this->save();
    }

    public function dataSort($field, $order)
    {
        $xOrder = $order == 'asc' ? 'desc' : 'asc';
        $img = $order == 'desc' ? '<img src=\'/data/admin/down.jpg\' />' : ' <img src=\'/data/admin/up.jpg\' />';
        $url = anchor('relax_articles_comments/sort/' . $field . '/' . $xOrder, t($field) . $img);
        $_SESSION['sm_sort_relax_articles_comments'] = array(
            'field' => $field,
            'order' => $order,
            'url' => $url);
    }

    public function dataIndex($target)
    {
        $numshow = 25;
        $this->db->select('relax_articles_comments.id, relax_articles_comments.fullname, relax_articles_comments.email, relax_articles_comments.status, relax_articles_comments.created, relax_articles.title as aid_type');
        $this->db->from('relax_articles_comments');

        $this->db->join('relax_articles', 'relax_articles_comments.aid = relax_articles.id');

        if ($this->session->userdata('sm_filter_relax_articles')) {
            $this->db->join('relax_categories', 'relax_categories.id = relax_articles.cid');
            $this->db->where('relax_categories.id', $this->session->userdata('sm_filter_relax_articles'));
        }
            
        if ($this->session->userdata('sm_find_relax_articles_comments')) {
            $this->db->like('relax_articles_comments.title', $this->session->userdata('sm_find_relax_articles_comments'));
        }
        if (isset($_SESSION['sm_sort_relax_articles_comments'])) {
            $sort = $_SESSION['sm_sort_relax_articles_comments'];
            $this->db->order_by('relax_articles_comments.' . $sort['field'], $sort['order']);
        } else {
            $this->db->order_by('relax_articles_comments.created', 'desc');
        }
        $this->db->limit($numshow, $target);
        $query = $this->db->get();
        $paging = paging_admin('relax_articles_comments/index', $this->_total(), $numshow, 4);
        return array(
            'query' => $query,
            'paging' => $paging,
            'fil_relax_articles' => $this->Filter_relax_articles(),
            'formFind' => $this->formFind());
    }

    private function cbo_relax_articles($id = 0)
    {
        $this->load->helper("form");
        $query = $this->db->get("relax_articles");
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $sl = $id == 0 ? set_value("aid") : $id;
        return form_dropdown("aid", $options, $sl);
    }

    private function _total()
    {
        return $this->db->count_all_results("relax_articles_comments");
    }

    private function _checkrelax_articles_commentsById($id)
    {
        $query = $this->db->get_where("relax_articles_comments", array("id" => $id));
        if ($query->num_rows() > 0)
            return true;
        else {
            notify_set_messages(notify_atten(t("Record không tồn tại")), "relax_articles_comments");
            return false;
        }
    }

    private function formFind()
    {
        $this->load->helper('form');
        $attributes = array('name' => 'frmFind', 'id' => 'frmFind');
        $f = form_open(site_url('relax_articles_comments/find'), $attributes);
        $data = array(
            'name' => 'txt_find',
            'id' => 'txt_find',
            'value' => '',
            'style' => 'width:200px');
        $f .= form_input($data);
        $s = array(
            'name' => 'submit',
            'value' => t('Find'),
            'class' => 'ibutton green',
            'style' => 'margin-left:10px');
        $f .= form_submit($s);
        if ($this->session->userdata('sm_find_relax_articles_comments')) {
            $r = array(
                'name' => 'btnReset',
                'value' => 'Reset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }

    private function Filter_relax_articles()
    {
        $this->load->helper('form');
        $query = $this->db->get('relax_categories');
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $id = !$this->session->userdata('sm_filter_relax_articles') ? 0 : $this->session->userdata('sm_filter_relax_articles');
        $attributes = array('name' => 'frmFilterrelax_articles', 'id' => 'frmFilterrelax_articles');
        $f = form_open(site_url('relax_articles_comments/filterrelax_articles'), $attributes);
        $f .= form_dropdown('aid', $options, $id, 'style="width:250px"');
        $data = array(
            'name' => 'btnFilterrelax_articles',
            'class' => 'ibutton green',
            'style' => 'margin-left:10px',
            'value' => t('Filter'));
        $f .= form_submit($data);
        if ($this->session->userdata('sm_filter_relax_articles')) {
            $r = array(
                'name' => 'btnReset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px',
                'value' => 'Reset');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }


    public function dataEdit($id)
    {
        if (!$this->_checkrelax_articles_commentsById($id)) {
           return redirect(site_url('relax_articles_comments')); 
        }  
        $sql = "SELECT c.*, a.title, rc.title as ctitle
                FROM relax_articles_comments c
                INNER JOIN relax_articles a ON a.id = c.aid
                INNER JOIN relax_categories rc ON  rc.id = a.cid
                WHERE c.id = ?";
        $query = $this->db->query($sql, array($id));
        return array('query' => $query, 'cbo_relax_articles' => $this->cbo_relax_articles($query->row('aid')));
    }

    public function dataAdd()
    {
        return array('cbo_relax_articles' => $this->cbo_relax_articles());
    }

    public function checkForm()
    {
        //$this->form_validation->set_rules('fullname', t('fullname'), 'trim|required');
        //$this->form_validation->set_rules('email', t('email'), 'trim|required');
        //$this->form_validation->set_rules('contents', t('contents'), 'trim|required');
        $this->form_validation->set_rules('status', t('status'), 'trim');
        //$this->form_validation->set_rules('created', t('created'), 'trim|required|integer');
        //$this->form_validation->set_rules('aid', t('aid'), 'trim|required|integer');
        return $this->form_validation->run();
    }

    public function doUpdate()
    {
        $this->load->helper('text');
        //$this->setFullname($this->input->post('fullname'));
        //$this->setEmail($this->input->post('email'));
        //$this->setContents($this->input->post('contents'));
        $this->setStatus($this->input->post('status'));
        //$this->setCreated($this->input->post('created'));
        //$this->setAid($this->input->post('aid'));

        if (isset($_POST['idx'])) {
            $id = decrypt($_POST['idx']);
            $this->setId($id);
            $this->setChanged(time());
        } 
        return $this->save();
    }

}
